package com.xxx.controller;

import com.xxx.listener.SystemListener;
import com.xxx.pojo.bean.ResponseDTO;
import com.xxx.pojo.entity.User;
import com.xxx.pojo.entity.UserLevel;
import com.xxx.service.UserLevelService;
import com.xxx.service.UserService;
import com.xxx.utils.JsonUtil;
import com.xxx.utils.RedisUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class TestController {

    @Resource
    private UserService userService;
    @Resource
    private UserLevelService userLevelService;

    @GetMapping("/hello")
    public String hello() {
        return "Hello SpringBoot3!";
    }

    /**
     * 系统启动时间戳
     */
    @RequestMapping("/versionNo")
    public Map<String, String> versionNo(){
        log.info("enter method versionNo().");
        Map<String, String> map = new HashMap<>();
        map.put("project", "springboot3");
        map.put("versionNo", SystemListener.versionNo);
        return map;
    }

    /**
     * 测试mysql
     */
    @RequestMapping("/testDb")
    public ResponseDTO testDb(String userId){
        log.info("enter method testDb().");
        User user = userService.lambdaQuery().eq(User::getUserId, userId).one();
        if(user!=null){
            user.setPwd(null);
            user.setInviteCode(null);
            user.setInvitedCode(null);
        }

        List<UserLevel> userLevelList = userLevelService.lambdaQuery().eq(UserLevel::getUserId, userId).list();
        return ResponseDTO.builtSuccess(userLevelList);
    }

    /**
     * 测试redis
     */
    @RequestMapping("/setRedis")
    public ResponseDTO setRedis(){
        log.info("enter method setRedis().");
        User user = new User();
        user.setUserId("1");
        user.setUserName("guweiqiang");
        user.setUserMobile("18600017641");
        user.setUserEmail("test@abc.com");
        String json = JsonUtil.encodeJson(user);
        RedisUtil.set("user", json, 600);
        return ResponseDTO.builtSuccess(user);
    }

    /**
     * 测试redis
     */
    @RequestMapping("/getRedis")
    public ResponseDTO getRedis(HttpServletRequest request){
        log.info("enter method getRedis().");

        String redisKey = request.getParameter("key");
        if(StringUtils.isEmpty(redisKey)) {
            return ResponseDTO.builtFail("key is null");
        }
        String json = RedisUtil.get(redisKey);
        if(StringUtils.isEmpty(json)) {
            return ResponseDTO.builtFail("json is null");
        }
        User userInfo = JsonUtil.decodeJson(json, User.class);
        return ResponseDTO.builtSuccess(userInfo);
    }



}
